/* Copyright 2012 Nathan Begle
    This file is part of pcoursemanager.

    pcoursemanager is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    pcoursemanager is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with pcoursemanager.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.MenuItem;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class Course implements WindowListener {
	Class_Connection connection;
	String course;
	Course_Window window;
	MenuItem item;
	
	int course_year, course_semester;
	long course_crn;
	
	public Course(int year, int semester, long crn, ActionListener action_listener)
		throws Exception
	{
		course_year = year;
		course_semester = semester;
		course_crn = crn;
		
		course = setCourseString(year, semester, crn);
		connection = new Class_Connection(crn, semester, year);
		window = null;
		item = create_Menu_Item(action_listener, course);
	}
	
	private MenuItem create_Menu_Item(ActionListener al, String text)
	{
		MenuItem tempItem = new MenuItem();
		
		tempItem.setLabel(text);
		tempItem.setEnabled(true);
		tempItem.setActionCommand(text);
		tempItem.addActionListener(al);
		
		return tempItem;
	}
	
	public String setCourseString(int year, int semester, long crn)
	{
		String temp = null;
		
		if(semester == 1)
		{
			temp = String.format("Fall %d - %d", year, crn);
		}
		else if(semester == 2)
		{
			temp = String.format("Spring %d - %d", year, crn);
		}
		else if(semester == 3)
		{
			temp = String.format("Summer %d - %d", year, crn);
		}
		return temp;
	}
	
	public String getCourseString()
	{
		return course;
	}
	
	public MenuItem getMenuItem()
	{
		return item;
	}
	
	public void open_window(ActionListener al)
	{
		if(window == null)
		{
			window = new Course_Window(this, al, course, course_year, course_semester, course_crn, connection.capacity, connection.actual, connection.remaining);
		} else
		{
			window.bring_to_front();
		}
	}
	
	public void remove_window()
	{
		window.removeWindow();
	}
	
	void update()
		throws Exception
	{
		connection.get_data();
		//connection.print(course_semester, course_year, course_crn);
		if(window != null)
		{
			window.update(connection.capacity, connection.actual, connection.remaining);
		}
	}

	public void windowActivated(WindowEvent arg0) {}
	public void windowClosed(WindowEvent arg0) {}
	public void windowClosing(WindowEvent arg0) {
		remove_window();
		window = null;
	}
	public void windowDeactivated(WindowEvent arg0) {}
	public void windowDeiconified(WindowEvent arg0) {}
	public void windowIconified(WindowEvent arg0) {}
	public void windowOpened(WindowEvent arg0) {}
}
